const express = require('express');
const router = express.Router();
const multer = require("multer");
const path = require("path");
const db = require("../model/db");


// 查询该宠主的详细信息
router.get("/:id", async (req, res) => {
    const data = await db.Host.findById(req.params.id);
    res.send({
        code: 0,
        message: "",
        data
    });
})


// 添加宠主
router.post("/", async (req, res) => {
    const data = await db.Host.create(req.body);
    res.send({
        code: 0,
        message: "",
        data
    });
})


//修改宠主信息
router.put("/:id", async (req, res) => {
    await db.Host.updateOne({
        _id: db.ObjectId(req.params.id)
    }, req.body); //(要修改的对象)
    res.send(true);
})


// 注销（删除）宠主
router.delete("/:id", async (req, res) => {
    await db.Host.deleteOne({
        _id: req.params.id
    });
    res.send(true);
})


// 得到所有宠主
router.get("/", async (req, res) => {
    const page = parseInt(req.query.page);
    const limit = parseInt(req.query.limit);
    let contion = {}; //条件对象
    if (req.query.key) {
        contion = {
            name: req.query.key.toUpperCase()
        }
    }
    let condition = {};
    if (req.query.key) {
        const exp = new RegExp(req.query.key, "i");
        condition = {
            $or: [{
                nickname: {
                    $regex: exp
                }
            },
            {
                mobile: {
                    $regex: exp
                }
            },
            {
                name: {
                    $regex: exp
                }
            }
            ]
        };
    }
    const data = await db.Host.find(condition).skip((page - 1) * limit).limit(limit);
    const count = await db.Host.count(condition);
    res.send({
        code: 0,
        message: "",
        data,
        count
    });
})



// 查询地址
router.get("/address/:id", async (req, res) => {
    const data = await db.Host.find({
        _id: db.ObjectId(req.params.id)
    }, req.body);
    res.send(data[0].address);
});
//查询单个地址
router.get("/address/get/:id", async (req, res) => { 
    const data = await db.Host.findOne({
        "address._id": req.params.id
    });
    console.log(data)
    res.send({
        code: 0,
        message: "",
        data
    });
});


//添加、修改,删除地址
router.put("/addOreditOrdeleteAddress/:id", async (req, res) => {
    const data = await db.Host.updateOne({
        _id: req.params.id
    }, {
            address: req.body
        });
    res.send({
        code: 0,
        message: "",
        data
    });
});


// 查询地址
router.get("/pet/:id", async (req, res) => {
    const data = await db.Host.find({
        _id: db.ObjectId(req.params.id)
    }, req.body);
    res.send(data[0].pets);
});
//查询单个地址
router.get("/pet/get/:id", async (req, res) => {
    const data = await db.Host.findOne({
        "pets._id": req.params.id
    });
    console.log(data)
    res.send({
        code: 0,
        message: "",
        data
    });
});
//添加、修改,删除宠物
router.put("/addOreditOrdeletePet/:id", async (req, res) => {
    const data = await db.Host.updateOne({
        _id: req.params.id
    }, {
            pets: req.body
        });
    res.send({
        code: 0,
        message: "",
        data
    });
});


module.exports = router;